﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class index : System.Web.UI.Page 
{
    DataProvider datapro = new DataProvider();
    protected void Page_Load(object sender, EventArgs e)
    {
        lbshoppingcart.Text = "";
        if (!IsPostBack)
        {
            loadhomepage();
            if (Request.QueryString["page"] == null && Request.QueryString["cateid"] == null)
            {
                if (Request.QueryString["proid"] != null)
                {
                    LoadControlDefault("ucproduct_details", "views/ucproduct_details.ascx");
                }
                else if (Request.QueryString["Cart"] != null)
                {
                    lbshoppingcart.Text = "<iframe scrolling=\"no\" src=\"views/cartonline.aspx\" width=\"100%\" height=\"1300px\" frameborder=\"0\" name=\"if_Cart\"></iframe>";
                }
                else if (Request.QueryString["regiter"] != null && Request.QueryString["regiter"].ToString() == "true")
                {
                    lbshoppingcart.Text = "<iframe scrolling=\"no\" src=\"views/regiter.aspx\" width=\"100%\" height=\"800px\" frameborder=\"0\" name=\"if_Cart\"></iframe>";
                }
                else if (Request.QueryString["info"] != null && Request.QueryString["info"].ToString() == "true")
                {
                    lbshoppingcart.Text = "<iframe scrolling=\"no\" src=\"views/regiter.aspx?info=true\" width=\"100%\" height=\"800px\" frameborder=\"0\" name=\"if_Cart\"></iframe>";
                }
                else if (Request.QueryString["log"] != null && (Request.QueryString["log"].ToString() == "true" || Request.QueryString["log"].ToString() == "cart"))
                {
                    lbshoppingcart.Text = "<iframe scrolling=\"no\" src=\"views/login.aspx?log=" + Request.QueryString["log"].ToString() + "\" width=\"100%\" height=\"800px\" frameborder=\"0\" name=\"if_Cart\"></iframe>";
                }
                else
                    _LoadDefault();
            }
            else
            {
                LoadControlDefault("uchomes", "views/ucproducts.ascx");
            }
        }
    }

    private void loadhomepage()
    {
        load_cache_products_view();
        load_cache_leftmune_root();
        load_cache_leftmune();
        load_leftmenu();
    }
    public void load_cache_products_view()
    {
        DataTable dt_leftmenu = datapro.sp_select("sp_view_productview");
        Cache.Remove("products_view");
        Cache.Insert("products_view", dt_leftmenu);
    }
    public void load_cache_leftmune()
    {
        DataTable dt_leftmenu = datapro.sp_select("sp_categories_select_all");
        Cache.Remove("leftmenu");
        Cache.Insert("leftmenu", dt_leftmenu);
    }
    public void load_cache_leftmune_root()
    {
        DataTable dt_leftmenu = datapro.sp_select("sp_categories_select_all_root");
        Cache.Remove("leftmenu_root");
        Cache.Insert("leftmenu_root", dt_leftmenu);
    }
    public void load_leftmenu()
    {
        DataTable dt_leftmenu_root = (DataTable)Cache["leftmenu_root"];
        DataTable dt_leftmenu = (DataTable)Cache["leftmenu"];
        string strmenu = "<ul id=\"dhtmlgoodies_listMenu\">";
        int cateid = 0;
        string strul = "";
        bool activemenu;
        if (Request.QueryString["cateid"] != null)
        {
            cateid = int.Parse(Request.QueryString["cateid"].ToString());
        }
        if (dt_leftmenu_root.Rows.Count != 0)
        {
            for (int i = 0; i < dt_leftmenu_root.Rows.Count; i++)
            {
                //if (cateid == int.Parse(dt_leftmenu_root.Rows[i]["categoryid"].ToString()))
                //{
                //    strmenu = strmenu + "<li><a  class=\"activeMenuLink\" href=\"#\">" +
                //        dt_leftmenu_root.Rows[i]["categoryname"].ToString() + "</a>";
                //}
                //else
                //{
                    strmenu = strmenu + "<li><a href=\"#\">" +
                        dt_leftmenu_root.Rows[i]["categoryname"].ToString() + "</a>";
                //}
                if (dt_leftmenu.Rows.Count != 0)
                {
                    strul = "<ul {0}>";
                    activemenu = false;
                    for (int j = 0; j < dt_leftmenu.Rows.Count; j++)
                    {
                        if (dt_leftmenu.Rows[j]["parentid"].ToString() == 
                            dt_leftmenu_root.Rows[i]["categoryid"].ToString())
                        {
                            if (cateid == int.Parse(dt_leftmenu.Rows[j]["categoryid"].ToString()))
                            {
                                strul = strul + "<li><a class=\"activeMenuLink_\" href=\"index.aspx?cateid=" +
                                    dt_leftmenu.Rows[j]["categoryid"].ToString() + "\">" +
                                    dt_leftmenu.Rows[j]["categoryname"].ToString() + "</a></li>";
                                activemenu = true;
                            }
                            else
                            {
                                strul = strul + "<li><a href=\"index.aspx?cateid=" +
                                    dt_leftmenu.Rows[j]["categoryid"].ToString() + "\">" +
                                    dt_leftmenu.Rows[j]["categoryname"].ToString() + "</a></li>";
                            }
                        }
                    }
                    if (activemenu == true)
                    {
                        if (i == 0)
                            strmenu = strmenu + string.Format(strul, "style='display: none;'") + "</ul>";
                        else
                            strmenu = strmenu + string.Format(strul, "style='display: block;'") + "</ul>";
                    }
                    else
                    {
                        if (i == 0)
                            strmenu = strmenu + string.Format(strul, "style='display: block;'") + "</ul>";
                        else
                        strmenu = strmenu + string.Format(strul, "") + "</ul>";
                    }
                }
                strmenu = strmenu + "</li>";
            }
            strmenu = strmenu + "</ul>";
        }

        lbmenu.Text = strmenu;
    }
    private void _LoadDefault()
    {
        LoadControlDefault("ucproducts", "views/uchomes.ascx");

    }
    private void LoadControlDefault(string IDControl, string pathControl)
    {
        try
        {
            Control control = (Control)this.LoadControl(pathControl);
            control.ID = IDControl;
            if (control != null)
                content.Controls.Add(control);

            content.Visible = true;
        }
        catch (System.Exception e)
        {
            //  throw e;
        }
    }
}